// pages/add/add.js
import Toast from '@vant/weapp/toast/toast';
const data = require('../../utils/data')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    data: '',
    active: 1,
    version: [],
    color: [],
    num: 1,
    finColor: '',
    finVersion: ''
  },
  onChange(event) {
    this.setData({
      num: event.detail
    })
  },
  handleTrueAdd() {
    const app = getApp();
    app.globalData.shopList.push({
      // id: this.data.data.id,
      id: Date.now(),
      num: this.data.num,
      finColor: this.data.finColor,
      finVersion: this.data.finVersion,
      img: this.data.data.goods_slides[0].slide_url,
      name: this.data.data.header
    })
    Toast({
      type: 'success',
      message: '添加成功',
      onClose: () => {
        wx.navigateBack({
          delta: 1,
          success: (res) => {},
          fail: (res) => {},
          complete: (res) => {},
        })
      },
    });
  },
  handleTab(event) {
    const index = event.currentTarget.dataset.index;
    // 更新attr_list.version的数据
    this.setData({
      version: this.data.version.map((item, idx) => ({
        ...item,
        selected: idx === index // 只有被点击的元素selected为true
      }))
    });
    // 获取被点击元素的值
    const selectedValue = this.data.data.attr_list.version[index].value;
    this.setData({
      finVersion: selectedValue
    })
  },
  handleTabColor(event) {
    const index = event.currentTarget.dataset.index;
    // 更新attr_list.color
    this.setData({
      color: this.data.color.map((item, idx) => ({
        ...item,
        selected: idx === index // 只有被点击的元素selected为true
      }))
    });
    // 获取被点击元素的值
    const selectedValue = this.data.data.attr_list.color[index].value;
    this.setData({
      finColor: selectedValue
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      data: data.default.goodsDetail.filter(ele => ele.id == options.id)[0],
      // finVersion: data.default.goodsDetail.default,
      // 遍历版本配置数组，添加selected属性
      version: data.default.goodsDetail.filter(ele => ele.id == options.id)[0].attr_list.version.map((item, index) => ({
        ...item,
        selected: index === 0
      })),
      // 遍历颜色配置数组，添加selected属性
      color: data.default.goodsDetail.filter(ele => ele.id == options.id)[0].attr_list.color.map((item, index) => ({
        ...item,
        selected: index === 0
      })),
      // 初始化默认值
      finColor: data.default.goodsDetail.filter(ele => ele.id == options.id)[0].attr_list.color[0]?.value,
      finVersion: data.default.goodsDetail.filter(ele => ele.id == options.id)[0].attr_list.version[0].value
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})